import sys

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        maxProfitBefore = []
        minPriceBefore = sys.maxsize
        maxProfit = 0
        for price in prices:
            minPriceBefore = min(minPriceBefore, price)
            maxProfit = max(price-minPriceBefore, maxProfit)
            maxProfitBefore.append(maxProfit)

        maxProfitAfter = []
        maxPriceAfter = -sys.maxsize
        maxProfit = 0
        for price in reversed(prices):
            maxPriceAfter = max(maxPriceAfter, price)
            maxProfit = max(maxPriceAfter-price, maxProfit)
            maxProfitAfter.append(maxProfit)
        
        maxProfitAfter.reverse()

        maxProfit = 0
        for profit in maxProfitBefore:
            maxProfit = max(maxProfit, profit)
        for profit in maxProfitAfter:
            maxProfit = max(maxProfit, profit)
        for i in range(len(prices)-1):
            maxProfit = max(maxProfit, maxProfitBefore[i] + maxProfitAfter[i+1])
        return maxProfit

s = Solution()
print(s.maxProfit([3,2,6,5,0,3]))